package com.controller.purchase;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.StringUtils;
import com.common.http.HttpResult;
import com.common.model.Global;
import com.common.spring.SpringMvcUtils;
import com.service.purchase.ReceiveGoodService;
import com.model.purchase.ReceiveGoodModel;
import com.model.sys.SysUser;

/**
 * 价格维护
 */
@Controller("receiveGoodController")
@RequestMapping(value = "/api")
public class ReceiveGoodController {
	static final Logger logger = LoggerFactory.getLogger(ReceiveGoodController.class);

	@Autowired
	ReceiveGoodService receiveGoodService;

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "purchase/receive/good/selectList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectList(@RequestParam Map<String, Object> map) {
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
			if (StringUtils.isNotNull(sysUser.getDatapermission())) {
			} else {
			}
			map.put("createId", sysUser.getUserid());
		}
		return receiveGoodService.selectList(map);
	}

	/**
	 * 查询六大费用收货单列表
	 *
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "purchase/receive/good/selectSixCostReceiveList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectSixCostReceiveList(@RequestParam Map<String, Object> map) {
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
			if (StringUtils.isNotNull(sysUser.getDatapermission())) {
			} else {
			}
			map.put("createId", sysUser.getUserid());
		}
		return receiveGoodService.selectSixCostReceiveList(map);
	}

	/**
	 * 查询详情
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "purchase/receive/good/selectInfor", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectInfor(@RequestParam String id) {
		return receiveGoodService.selectInfor(id);
	}

	/**
	 * 保存询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "purchase/receive/good/save", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult save(@RequestBody ReceiveGoodModel receiveGoodModel) {
		return receiveGoodService.save(receiveGoodModel);
	}

	/**
	 * 复制询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "purchase/receive/good/copy", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult copy(@RequestBody ReceiveGoodModel receiveGoodModel) {
		return receiveGoodService.copy(receiveGoodModel);
	}

	/**
	 * 删除询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "purchase/receive/good/delete", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delete(@RequestParam String id) {
		return receiveGoodService.delete(id);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "purchase/receive/good/export")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		receiveGoodService.export(map, response);

	}

	@RequestMapping(value = "purchase/receive/good/exportSix")
	public void exportSix(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		receiveGoodService.exportSix(map, response);

	}
}
